package com.xying.util;

import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author: Hisen
 * @date: 2018/10/24 17:03
 * @description:
 */
public class FTPUtil {
  private static final Logger logger = LoggerFactory.getLogger(FTPUtil.class);

  private static String ftpIp = PropertiesUtil.getProperty("ftp.host");
  private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
  private static String ftpPassword = PropertiesUtil.getProperty("ftp.password");


  public FTPUtil(String ip, int port, String user, String pwd) {
    this.ip = ip;
    this.port = port;
    this.user = user;
    this.pwd = pwd;
  }

  public static boolean uploadFile(List<File> fileList) throws IOException{
    //传入ip，端口，用户和密码
    FTPUtil ftpUtil = new FTPUtil(ftpIp, 21, ftpUser, ftpPassword);
    logger.info("开始连接ftp服务器");
    boolean result = ftpUtil.uploadFile("img", fileList);
    logger.info("开始连接ftp服务器，结束上传，上传结果:{}", result);

    return result;
  }

  private boolean uploadFile(String remotePath, List<File> fileList) throws IOException {
    boolean uploaded = true;
    FileInputStream fis = null;
    //连接FTP服务器
    if(connectServer(this.ip, this.port, this.user, this.pwd)){
      try {
        //切换工作文件夹
        ftpClient.changeWorkingDirectory(remotePath);
        //设置缓存大小
        ftpClient.setBufferSize(1024);
        //设置编码格式
        ftpClient.setControlEncoding("UTF-8");
        //设置成二进制文件类型
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        //打开本地的被动模式
        ftpClient.enterLocalPassiveMode();

        //将文件传入流内，然后通过ftpClient上传
        for(File file : fileList){
          fis = new FileInputStream(file);
          ftpClient.storeFile(file.getName(), fis);
        }
      } catch (IOException e) {
        logger.error("上传文件异常", e);
        uploaded = false;
        e.printStackTrace();
      } finally {
        fis.close();
        ftpClient.disconnect();
      }
    }

    return uploaded;
  }

  /**
   * 连接ftp服务器
   * @param ip
   * @param port
   * @param user
   * @param pwd
   * @return
   */
  private boolean connectServer(String ip, int port, String user, String pwd){
    boolean isSuccess = false;

    ftpClient = new FTPClient();
    try {
      ftpClient.connect(ip);
      isSuccess = ftpClient.login(user, pwd);
    } catch (IOException e) {
      logger.error("连接FTP服务器异常",e);
      e.printStackTrace();
    }
    return isSuccess;
  }


  private String ip;
  private int port;
  private String user;
  private String pwd;
  private FTPClient ftpClient;
  public static String getFtpIp() {
    return ftpIp;
  }

  public static void setFtpIp(String ftpIp) {
    FTPUtil.ftpIp = ftpIp;
  }

  public static String getFtpUser() {
    return ftpUser;
  }

  public static void setFtpUser(String ftpUser) {
    FTPUtil.ftpUser = ftpUser;
  }

  public static String getFtpPassword() {
    return ftpPassword;
  }

  public static void setFtpPassword(String ftpPassword) {
    FTPUtil.ftpPassword = ftpPassword;
  }

  public String getIp() {
    return ip;
  }

  public void setIp(String ip) {
    this.ip = ip;
  }

  public int getPort() {
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public String getUser() {
    return user;
  }

  public void setUser(String user) {
    this.user = user;
  }

  public String getPwd() {
    return pwd;
  }

  public void setPwd(String pwd) {
    this.pwd = pwd;
  }

  public FTPClient getFtpClient() {
    return ftpClient;
  }

  public void setFtpClient(FTPClient ftpClient) {
    this.ftpClient = ftpClient;
  }
}
